<?php
FLEA::loadClass('FLEA_Db_TableDataGateway');

class	Model_User	extends		FLEA_Db_TableDataGateway
{
	var		$tableName		=	'user';
	var		$primaryKey		=	'UserID';

	/**
	 * 注册client
	 *
	 * @param array $client
	 */
	function 	clientRegister($business,$email,$password,$message="",$firstName="",$lastName="",$firmID=0)
	{
		$user	=	$this->findByField('Email',$email);
		if($user['UserID'] != '')
		{
			/**
			 * the email is used,try other
			 */
			return -1;
		}
		$_modelClient		=&	FLEA::getSingleton('Model_Client');
		$_modelInventor		=&	FLEA::getSingleton('Model_Inventor');

		$user['Email']		=	$email;
		$user['Password']	=	$password;
		$user['FirstName']	=	$firstName;
		$user['LastName']	=	$lastName;
			
		$userID				= 	$this->create($user);
			
		$client['UserID']	=	$userID;
		$client['Business']	=	$business;
			
		$clientID	=	$_modelClient->create($client);
		
		$_modelActionEvent	=&	FLEA::getSingleton("Model_ActionEvent");
		$_modelActionEvent->updateActionEvent($firmID,$clientID,"Add Client");
		
		$inventor['UserID']	=	$userID;
		$inventor['ClientID']=	$clientID;
			
		if($_modelInventor->create($inventor))
		{

			require("Extra/Mail.php");
			set_time_limit(100);

			$SmtpEmailTo 	= $email;

			$mailsubject 	= 	"[IP Manager] ------";
			$mailHeader		=	"Content-Type:text/HTML ";
			if($message == "")
			{
				$mailBody		=	"<div style='font-family: \"Times New Roman\", Times, serif;color:#000000;font-size:14px;
			font-weight:800;'>
			Welcome to IP Manager, where growing businesses manage their intellectual property.  
			At IP Manager, you can record your ideas, estimate the value of an idea, request bids from law firms for 
			preparing patent applications, and track the application process.<br><br>
			You have successfully registered your business with this email address as your user id. 
			 You may login <a href='".APP_NAME."'>here</a> with the password that you entered during registration.  If you have forgotten your password, 
			you may have a new password sent to you by selecting reset.<br><br>
			IP Manager does not provide legal services, but can connect you with providers of legal services.<br>
			 
			</div>";
			}
			else
			{
				$mailBody	=	"<div style='font-family: \"Times New Roman\", Times, serif;color:#000000;font-size:14px;
			font-weight:800;'>".$message."</div>";
			}
			$smtp = new smtp(SmtpServer,Smtpserverport,true,SmtpUser,SmtpPass);
			$smtp->debug = true;


			$re				=
			$smtp->sendmail($SmtpEmailTo,SmtpUserMail,$mailsubject,$mailBody,"","","",$mailHeader);
		}

		return 1;
	}
	/**
	 * 注册从业者
	 *
	 * @param array $practitioner
	 */
	function 	practitionerRegister($name,$email,$password)
	{
		$user	=	$this->findByField('Email',$email);
		if($user['UserID'] != '')
		{
			/**
			 * the email is used,try other
			 */
			return 0;
		}
		$_modelFirm			=&	FLEA::getSingleton('Model_Firm');
		$_modelPractitioner	=&	FLEA::getSingleton('Model_Practitioner');

		$user['Email']		=	$email;
		$user['Password']	=	$password;
			
		$userID				= 	$this->create($user);
			
		$firm['UserID']		=	$userID;
		$firm['Name']		=	$name;
			
		$firmID	=	$_modelFirm->create($firm);
			
		$practitioner['UserID']	=	$userID;
		$practitioner['FirmID']	=	$firmID;
			


		if($_modelPractitioner->create($practitioner))
		{

			require("Extra/Mail.php");
			set_time_limit(100);

			$SmtpEmailTo 	= $email;

			$mailsubject 	= 	"[IP Manager] ------";
			$mailHeader		=	"Content-Type:text/HTML ";

			$mailBody		=	"<div style='font-family: \"Times New Roman\", Times, serif;color:#000000;font-size:14px;
			font-weight:800;'>Welcome to IP Manager.<br><br>
			IP Manager links you to growing businesses, and allows you to bid for their patent applications as well as provide status information to clients.<br><br>
			You have successfully registered your firm with this email address as your user id.  You may login here with the password you entered during registration.  If you have forgotten your password, you may have a new password sent to you by selecting reset.
			
			</div>";

			$smtp = new smtp(SmtpServer,Smtpserverport,true,SmtpUser,SmtpPass);
			$smtp->debug = true;


			$re				=
			$smtp->sendmail($SmtpEmailTo,SmtpUserMail,$mailsubject,$mailBody,"","","",$mailHeader);
		}

		return 1;
	}
	/**
	 * 修改用户密码
	 *
	 * @param int $userID
	 * @param string $oldPassword
	 * @param string $newPassword
	 * @return int
	 */
	function 	changePassword($userID,$oldPassword,$newPassword)
	{
		$user	=	$this->find("UserID=".$userID." and Password='".$oldPassword."'");
		if($user["UserID"] == $userID)
		{
			$user['Password'] = $newPassword;
			if( $this->save($user) )
			{
				/**
				 * successfully change password
				 */
				return 1;
			}
			else
			{
				/**
				 * Can't change password now.
				 */
				return 0;
			}
		}
		else
		{
			/**
			 * old password is not correct
			 */
			return -1;
		}
	}
	/**
	 * edit account of the user
	 *
	 * @param array $user
	 * @return int
	 */
	function	editAccount($user)
	{
		return $this->save($user);
	}
	/**
	 * 获得用户全部信息
	 *
	 * @param int $userID
	 * @return array
	 */
	function	findUser($userID)
	{
		$userID	=	$userID>0?$userID:0;
		$user 	=	$this->findByField('UserID',$userID);
		
		$_modelClient	=&	FLEA::getSingleton('Model_Client');
		$_modelInventor	=&	FLEA::getSingleton('Model_Inventor');
		$_modelFirm		=&	FLEA::getSingleton('Model_Firm');
		$_modelPractitioner=&FLEA::getSingleton('Model_Practitioner');
		
		$client			=	$_modelClient->findByField('UserID',$userID);
		$firm			=	$_modelFirm->findByField('UserID',$userID);
		
		$inventor		=	$_modelInventor->findByField('UserID',$userID);
		$practitioner	=	$_modelPractitioner->findByField('UserID',$userID);
		
		if($inventor['InventorID'] != '')
		{
			$client		=	$_modelClient->findByField('ClientID',$inventor['ClientID']);
		}
		
		if($practitioner['FirmID'] != '')
		{
			$firm		=	$_modelFirm->findByField('FirmID',$practitioner['FirmID']);
		}
		$user['client']		=	$client;
		$user['inventor']	=	$inventor;
		$user['firm']		=	$firm;
		$user['practitioner']=	$practitioner;
		
		return $user;
	}
	/**
	 * 保存用户信息
	 *
	 * @param array $user
	 * @return int
	 */
	function	saveUser($user)
	{
		return $this->save($user);
	}
	/**
	 * 客户登陆
	 *
	 * @param string $email
	 * @param string $password
	 */
	function 	userLogin($email,$password)
	{	
		$sql	=	
			"
				select user.IPManager , user.UserID,user.Email,user.Password,client.ClientID,inventor.InventorID,firm.FirmID,practitioner.PractitionerID from user 
				left join client on client.UserID=user.UserID 
				left join inventor on inventor.UserID=user.UserID 
				left join firm on firm.UserID=user.UserID 
				left join practitioner on practitioner.UserID=user.UserID
				where user.Email='".$email."'"
			;
		$user	=	$this->findBySql($sql);
		return $user[0];
	}
}
?>